﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Threading;

namespace PublicComputerLogoffApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        DispatcherTimer logoffCountdownTimer = new DispatcherTimer();
        int secondsOnCountdownTimer = -1;
        string errorSource = string.Empty;
        string errorMessage = string.Empty;

        public MainWindow()
        {
            this.Width = System.Windows.SystemParameters.WorkArea.Width;
            this.Height = System.Windows.SystemParameters.WorkArea.Height;
            this.Left = 0;
            this.Top = 0;
            this.WindowState = WindowState.Normal;
            this.Hide();
            SolidColorBrush windowBackground = new SolidColorBrush();
            windowBackground.Opacity = 0.9;
            windowBackground.Color = (Color)ColorConverter.ConvertFromString(Properties.Settings.Default.BackgroundColor);
            this.Background = windowBackground;
            initializeTimer();
            InitializeComponent();
            monitorIdleTime(10);
        }

        private void monitorIdleTime(uint threshold)
        {
            try
            {
                do
                {
                    Thread.Sleep(1000);
                }
                while (LastUserInput.SecondsSinceLastUserInput < threshold);
                this.Show();
                updateUiTimer();
                startTimer();
            }
            catch(Exception e)
            {
                displayError(e);
            }
        }

        private void quitApplication()
        {
            Application.Current.Shutdown();
        }

        private void continueSessionButton_Click(object sender, RoutedEventArgs e)
        {
            logoffCountdownTimer.Stop();
            this.Hide();
            monitorIdleTime((uint)Properties.Settings.Default.IdleDurationTimer);
        }

        private void logoffButton_Click(object sender, RoutedEventArgs e)
        {
            UserLogoff.LogoffUser();
        }

        private void startTimer()
        {
            secondsOnCountdownTimer = Properties.Settings.Default.CountdownTimerDuration;
            updateUiTimer();
            logoffCountdownTimer.Start();
        }

        private void initializeTimer()
        {
            logoffCountdownTimer.Interval = new TimeSpan(0, 0, 1);
            logoffCountdownTimer.Tick += logoffCountdownTimer_Tick;
        }

        private void logoffCountdownTimer_Tick(object sender, EventArgs e)
        {
            secondsOnCountdownTimer--;
            updateUiTimer();

            if (secondsOnCountdownTimer == 0)
            {
                UserLogoff.LogoffUser();
            }
        }

        private void updateUiTimer()
        {
            this.timer.Content = secondsOnCountdownTimer < 10 ? "0" + secondsOnCountdownTimer.ToString() : secondsOnCountdownTimer.ToString();
        }

        private void image_Loaded(object sender, EventArgs e)
        {
            string sourceLocation = string.Format(
                "{0}\\Images\\{1}",
                Environment.CurrentDirectory,
                Properties.Settings.Default.LogoImageFilename);

            BitmapImage b = new BitmapImage();
            b.BeginInit();
            b.UriSource = new Uri(sourceLocation);
            b.EndInit();

            var image = sender as Image;
            image.Source = b;
        }

        private void titleText_Loaded(object sender,EventArgs e)
        {
            windowTitle.Content = string.IsNullOrEmpty(errorSource) ? Properties.Settings.Default.TitleText : errorSource;
        }

        private void bodyText_Loaded(object sender, RoutedEventArgs e)
        {
            bodyText.Text = string.IsNullOrEmpty(errorMessage) ? Properties.Settings.Default.MessageText : errorMessage;
        }

        private void displayError(Exception e)
        {
            errorSource = e.Source;
            errorMessage = e.Message;
            this.Show();
        }
    }
}
